package com.dg.cuckoo.admin.service.impl;

import com.dg.cuckoo.admin.dto.earlyProject.TwoEarlyProjectDto;
import com.dg.cuckoo.admin.dto.other.FileDto;
import com.dg.cuckoo.admin.repository.FileRepository;
import com.dg.cuckoo.admin.repository.TwoEarlyProjectRepository;
import com.dg.cuckoo.admin.service.ApprovalService;
import com.dg.cuckoo.admin.service.DesignApprovalService;
import com.dg.cuckoo.admin.service.FileService;
import com.dg.cuckoo.admin.service.ProjectInfoServer;
import com.dg.cuckoo.admin.vo.PageDto;
import com.dg.cuckoo.common.utils.PageModel;
import com.dg.cuckoo.core.ServiceException;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ApprovalServiceimpl implements ApprovalService {

    @Autowired
    private TwoEarlyProjectRepository repository;
    @Autowired
    private ProjectInfoServer projectInfoServer;
    @Autowired
    private FileRepository fileRepository;
    @Autowired
    private FileService fileService;
    @Autowired
    private DesignApprovalService designApprovalService;

    @Override
    public void save(String projectId, TwoEarlyProjectDto dto, MultipartFile[] files) throws Exception {
        designApprovalService.powerfulSave(projectId,dto,files,1);
    }


    @Override
    public void delect(String id) {
        if(id==null){
            throw new ServiceException("请选择行");
        }
        repository.deleteById(id);
        fileRepository.deleteAll(fileRepository.findByByMark(id));
    }


    @Override
    public PageModel page(Integer page, Integer pageSize, String projectId) {
        return designApprovalService.powerfulPage(page,pageSize,projectId,1);
    }


    @Override
    public void update(String id, String projectId, MultipartFile[] files, TwoEarlyProjectDto dto) throws Exception {
        designApprovalService.powerfulUpdate(id,projectId,files,dto,1);
    }

    @Override
    public String openContentInfo(String id) {
        if(id==null){
            throw new ServiceException("请选择行");
        }
        return repository.findById(id).get().getContentBox();
    }

    @Override
    public String openInfo(String id) {
        TwoEarlyProjectDto dto = repository.findById(id).get();
        List<String> files = fileService.byByMark(id);
        JSONObject json = new JSONObject();
        json.put("dto",dto);
        json.put("files",files);
        return json.toString();
    }
}
